﻿using EWC.Core.Application.Dtos;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
using EWC.HIS.Application.Contracts.AppointmentRegistrationService.Dtos;
using EWC.HIS.Application.Contracts.AppointmentRegistrationService.Queries;
using EWC.HIS.Application.Contracts.AppointmentRegistrationService.Commands;

namespace EWC.HIS.WebApi.Controllers
{
    /// <summary>
    /// 门诊预约挂号登记
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class AppointmentRegistrationController : ControllerBase
    {

        private readonly IMediator _mediator;

        public AppointmentRegistrationController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 获取预约信息
        /// </summary>
        /// <returns>预约信息列表</returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<AppointmentRegistrationDto>>> GetList()
        {
            var query = new GetAppointmentRegistrationsQuery(new PagedRequestInput());
            var list = await _mediator.Send(query);
            return Ok(list);
        }

        /// <summary>
        /// 根据 ID 获取预约信息
        /// </summary>
        /// <param name="id">预约信息 ID</param>
        /// <returns>预约信息</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<AppointmentRegistrationDto>> GetById([NotNull] int id)
        {
            var query = new GetAppointmentRegistrationQuery(id);
            var appointmentRegistration = await _mediator.Send(query);
            return Ok(appointmentRegistration);
        }
    

        /// <summary>
        /// 添加预约信息
        /// </summary>
        /// <param name="createAppointmentRegistrationDto">预约信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromBody] CreateAppointmentRegistrationDto createAppointmentRegistrationDto)
        {
            var command = new AddAppointmentRegistrationCommand(createAppointmentRegistrationDto);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        /// <summary>
        /// 更新预约信息
        /// </summary>
        /// <param name="updateAppointmentRegistrationDto">预约信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromBody] UpdateAppointmentRegistrationDto updateAppointmentRegistrationDto)
        {
            var command = new UpdateAppointmentRegistrationCommand { RegistrationDto = updateAppointmentRegistrationDto };
            var result = await _mediator.Send(command);
            return Ok(result);

        }

        /// <summary>
        /// 删除预约信息
        /// </summary>
        /// <param name="id">预约信息 ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            var command = new DeleteAppointmentRegistrationCommand(id);
            var result = await _mediator.Send(command);
            return Ok(result);

        }
    }
}
